﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class ContextualGroup : ICommandUIDefinition, IParentable, IXmlBuildable, IChildDeletable
    {
        public Enumerations.Color? Color { get; set; }
        public string Command { get; set; }
        public string ContextualGroupId { get; set; }
        public string Id { get; set; }
        public int? Sequence { get; set; }
        public string Title { get; set; }
        public List<Tab> Tabs { get; set; }

        public ContextualGroup()
        {
            this.Tabs = new List<Tab>();
        }

        public ContextualGroup(XmlNode node):this()
        {
            try
            {
                this.Color = (Enumerations.Color)Enum.Parse(typeof(Enumerations.Color), XmlHelper.GetAttribute(node, "Color"));
                this.Command = XmlHelper.GetAttribute(node, "Command");
                this.ContextualGroupId = XmlHelper.GetAttribute(node, "ContextualGroupId");
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.Sequence = XmlHelper.GetNullableIntAttribute(node, "Sequence");
                this.Title = XmlHelper.GetAttribute(node, "Title");

                foreach (XmlNode tabNode in node.SelectNodes("Tab"))
                {
                    if (tabNode.NodeType == XmlNodeType.Comment)
                        return;

                    Tab tab = new Tab(tabNode);
                    this.Tabs.Add(tab);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode contextualGroupNode = XmlHelper.AddNode(commandUINode, "ContextualGroup");

                XmlHelper.AddAttribute(contextualGroupNode, "Color", this.Color);
                XmlHelper.AddAttribute(contextualGroupNode, "Command", this.Command);
                XmlHelper.AddAttribute(contextualGroupNode, "ContextualGroupId", this.ContextualGroupId);
                XmlHelper.AddAttribute(contextualGroupNode, "Id", this.Id);
                XmlHelper.AddAttribute(contextualGroupNode, "Sequence", this.Sequence);
                XmlHelper.AddAttribute(contextualGroupNode, "Title", this.Title);

                foreach (Tab tab in this.Tabs)
                {
                    tab.AddNode(contextualGroupNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("ContextualGroup");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "Tab" }, true);

                foreach (Tab tab in this.Tabs)
                {
                    tab.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is Tab))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    if (this.Tabs == null)
                    {
                        this.Tabs = new List<Tab>();
                    }

                    this.Tabs.Add((Tab)child);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is Tab)
                {
                    this.Tabs.Remove((Tab)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
